package com.gx.smart.building.ui.swap.fragment

import android.view.ViewGroup
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentSwapWorkBinding
import com.gx.smart.building.ui.swap.adapter.PageAdapter
import com.gx.smart.building.ui.swap.viewmodel.SwapWorkViewModel

class SwapWorkFragment : BaseMVVMFragmentV2<SwapWorkViewModel>() {

    private lateinit var viewBinding: FragmentSwapWorkBinding
    var position: Int = 0
    override fun onBindLayout(): Int = R.layout.fragment_swap_work

    override fun getToolbarTitle(): String = "交接班"

    override fun onBindViewModel(): Class<SwapWorkViewModel> = SwapWorkViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        position = requireActivity().intent.getIntExtra("position", 0)
        viewBinding = FragmentSwapWorkBinding.inflate(layoutInflater, root, true)
        val pageAdapter = PageAdapter(requireActivity())
        viewBinding.viewPager.adapter = pageAdapter
        viewBinding.viewPager.isUserInputEnabled = true
        val titles = resources.getStringArray(R.array.swap_work)
        val mediator = TabLayoutMediator(
            viewBinding.tabLayout, viewBinding.viewPager
        ) { tab: TabLayout.Tab, position: Int ->
            tab.text = titles[position]
        }
        mediator.attach()
        viewBinding.viewPager.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                this@SwapWorkFragment.position = position
            }
        })
        viewBinding.viewPager.currentItem = position
    }


}